// const Screen_Width = document.body.clientWidth
import {getConfiguration} from './configuration';
import _ from 'lodash';
import Images from './Images';

const Screen_Width = window.screen.width;
const Screen_Height = window.screen.height;

const Metrics = {
  borderColor: '#f1f1f1',
  borderWidth: 0.5,
  textLightColor: '#999',
  Screen_Width,
  Screen_Height,
  bankCardCodeRegex: /^([1-9]{1})(\d{14}|\d{18})$/,      // 银行卡号校验
  phoneNumberRegex: /^1\d{10}$/,         // 手机号校验
  IdNumberRegex: /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/,    // 身份证号验证
};

export default Metrics;

export const lotteryTypeDict = {
  //瞎猜
  blind_guess: {
    guess_title: '瞎猜',

  },
  //乱猜
  mess_guess: {
    guess_title: '乱猜'
  },
  //胡猜
  bullshit_guess: {
    guess_title: '胡猜'
  },
  //随便猜
  anyway_guess: {
    guess_title: '随便猜'
  }
}

//排序算法
export const arrSort = (name) => {
  return (o, p) => {
    let a, b;
    if (typeof o === "object" && typeof p === "object" && o && p) {
      a = o[name];
      b = p[name];
      if (a === b) {
        return 0;
      }
      if (typeof a === typeof b) {
        return a < b ? 1 : -1;
      }
      return typeof a < typeof b ? 1 : -1;
    }
    else {
      throw ("error");
    }
  }
}


export function font(size) {
  return `${Screen_Width / 750 * size}px`;
}

export const URL_ROOT = 'http://www.xoguanjia.com';
// export const URL_ROOT = 'http://192.168.6.103'


export const URL_UNVerify = `${URL_ROOT}/bbt/yllogin/auth.do`;

export function url(path) {
  const apiRoot = URL_ROOT;
  return path.indexOf('/') === 0
    ? apiRoot + path
    : `${apiRoot}/${path}`;
}

export function get_alipay_url(parmas) {
  const url = url('/bbt/api/pay/alipay');
  const fin_url = url + get_params_string(parmas);
  console.log('get_alipay_url', fin_url);
  return fin_url;
}

export function get_params_string(params) {
  let query_string = _.join(_.map(params, (v, k) => `${k}=${v}`), '&');
  query_string = query_string ? `?${query_string}` : query_string;
  console.log('get_params_string', query_string);
  return query_string;
}

// 代码片段来自网络
// 获取地址栏参数，只能是英文参数
export function GetQueryString(name) {
  const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`);
  const r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
}

// 获取地址栏参数，可以是中文参数
export function getUrlParam(key) {
  // 获取参数
  const url = window.location.search;
  // 正则筛选地址栏
  const reg = new RegExp(`(^|&)${key}=([^&]*)(&|$)`);
  // 匹配目标参数
  const result = url.substr(1).match(reg);
  // 返回参数值
  return result ? decodeURIComponent(result[2]) : null;
}

// 获取银行卡icon
export function getBankIcon(parms) {
  let imgSrc;
  if (`${_.get(Images, parms)}` == 'undefined' || _.isEmpty(parms)) {
    imgSrc = _.get(Images, 'bank_defult_icon');
  } else {
    imgSrc = _.get(Images, parms);
  }

  return (imgSrc);
}

/**
 * 封装阶乘函数
 * @param m       需要的个数
 * @param n       总个数（数组长度）
 * @returns {number}    排列组合的个数
 * conbination(5,11)   11个数字随机选5个数 不重复
 */
export function conbination(m, n) {
  function f(s) {
    if (s <= 0) return 1;
    return s * f(s - 1);
  }

  let y = (f(n) / f(m)) / f(n - m);
  return y;
}

/*
arr需要筛选的数组
num 需要选出的个数
return_array 返回新的数组
 */
export function getArrayItems(arr, num) {
  //新建一个数组,将传入的数组复制过来,用于运算,而不要直接操作传入的数组;
  let temp_array = new Array();
  for (let index in arr) {
    temp_array.push(arr[index]);
  }
  //取出的数值项,保存在此数组
  let return_array = new Array();
  for (let i = 0; i < num; i++) {
    //判断如果数组还有可以取出的元素,以防下标越界
    if (temp_array.length > 0) {
      //在数组中产生一个随机索引
      let arrIndex = Math.floor(Math.random() * temp_array.length);
      //将此随机索引的对应的数组元素值复制出来
      return_array[i] = temp_array[arrIndex];
      //然后删掉此索引的数组元素,这时候temp_array变为新的数组
      temp_array.splice(arrIndex, 1);
    } else {
      //数组中数据项取完后,退出循环,比如数组本来只有10项,但要求取出20项.
      break;
    }
  }
  return return_array;
}
//随机数
export function shuffleFrenzy(arr,num,tex,isSelec) {
  let length = arr.length;
  let currArr = arr;
  let selectArr = [];
  let selIndex = null;
  let returnObj = {};
  let text= "text";
  let isSelect = "isSelect";
  if(tex){
    text = tex;
  }
  if(isSelec){
    isSelect = isSelec;
  }

    console.log(text);
    console.log(isSelect);
  for(let i=0; i<length; i++){
    currArr[i][isSelect] = false;
  }
console.log('currArr',currArr)
  for(let i=0; i<num;i++){
    selIndex = Math.floor(Math.random()*(length-i));
    if(currArr[selIndex][isSelect] != true){
      selectArr.push(currArr[selIndex][text]);
      currArr[selIndex][isSelect] = true;
    }else {
      i--;
      continue;
    }
  }
  returnObj.allArr = currArr;
  returnObj.selectArr = selectArr;
  return returnObj;
}
/**
 * 产生序列数组
 * @param num
 * @param color 字体颜色
 * @returns {Array}
 */
export  function createSequenceArr(num,color) {
  let arr = [];
  for(let i =1; i<= num; i++){
    let obj = {};
    obj.text = i<10?"0"+i:i+"";
    obj.isSelect = false;
    obj.color = color;
    arr.push(obj)
  }
  return arr;
}

/**
 * 随机一组数
 * @param maxNum      最大数  >0
 * @param num         随机次数
 * @returns {Array}   数组（可作为数组下标）
 */
export  function groupRandomly(maxNum, num) {
  let index;
  let curArr = [];
  let item;
  let i = num;
  while (i){
    index = Math.ceil(Math.random()*maxNum);
    item = index<10?"0"+index:index+"";
    if(curArr.indexOf(item) != -1){
      continue;
    }
    curArr.push(item);
    i--;
  }
  return curArr;
}


// 保存本地
export function setLocalStorage(key,value) {
  return localStorage.setItem(key, value);
}
export function getLocalStorage(key) {
  return localStorage.getItem(key);
}


